﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Kursovaya_Pav.St.Dm
{
    public partial class Info : Form
    {
       public Info()
        {
           InitializeComponent();
            idVal = "0";
            RefreshData();
        }

        public string idVal;

        private void button1_Click(object sender, EventArgs e)
        {
            int idrow = 1;
            string fieldSp = "namec";
            string tabSp = "Country";
            if (textBox1.Text != "")
            {
                Con_DB cdb = new Con_DB(@"DBdecor.accdb");
                switch (tabControl1.SelectedIndex)
                {
                    case 0:
                        tabSp = "Country";
                        fieldSp = "namec";
                        idrow = dataGridView1.SelectedCells[0].RowIndex;
                        // idVal = dataSet1.Tables[0].Rows[idrow][0].ToString();
                        break;
                    case 1:
                        tabSp = "Type_using";
                        fieldSp = "nameu";
                        idrow = dataGridView2.SelectedCells[0].RowIndex;
                       // idVal = dataSet2.Tables[0].Rows[idrow][0].ToString(); 
                        break;
                    case 2:
                        tabSp = "Move_info";
                        fieldSp = "namep";
                        idrow = dataGridView3.SelectedCells[0].RowIndex;
                       // idVal = dataSet3.Tables[0].Rows[idrow][0].ToString();
                        break;
                }
                if (idVal == "0")
                    cdb.SetData("Insert into " + tabSp + " (" + fieldSp + ") values ('" + textBox1.Text + "')");
                else cdb.SetData("Update " + tabSp + " SET " + fieldSp + " = '" + textBox1.Text + "' where ID = " + idVal);
                if (idVal == "0")
                   
                 cdb.SetData("Update " + tabSp + " SET " + fieldSp + " = '" + textBox1.Text + "' where ID = " + idVal);
                else cdb.SetData("Update " + tabSp + " SET " + fieldSp + " = '" + textBox1.Text + "' where ID = " + idVal);
                
                
                
               // cdb.SetData("DELETE FROM " + tabSp + " where ID = " + idVal+2);
                textBox1.Clear();
                textBox1.Focus();


                RefreshData();


               // Form3 ff = new Form3();
                //ff.Dispose();
            }
        }

        public void RefreshData()
        {
            Con_DB cdb = new Con_DB(@"DBdecor.accdb");
           dataSet1.Clear();
           // dataGridView1.Update();
           // dataGridView1.Columns.Clear();
            cdb.GetData("select * from Country", dataSet1);
            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = dataSet1;
            dataGridView1.DataMember = dataSet1.Tables[0].TableName;
            
            

            //Пример заполнения ComboBox
           
            //КОНЕЦ
            EnterData f4 = new EnterData();
           
           
                dataSet2.Clear();
            cdb.GetData("select * from Type_using", dataSet2);
            dataGridView2.Columns.Clear();
            dataGridView2.AutoGenerateColumns = true;
            dataGridView2.DataSource = dataSet2;
            dataGridView2.DataMember = dataSet2.Tables[0].TableName;
            dataGridView2.Update();

            dataSet3.Clear();
            cdb.GetData("select * from Move_info", dataSet3);
            dataGridView3.Columns.Clear();
            dataGridView3.AutoGenerateColumns = true;
            dataGridView3.DataSource = dataSet3;
            dataGridView3.DataMember = dataSet3.Tables[0].TableName;
            dataGridView3.Update();

            cdb.RefData();

            // ЭТУ ПРОЦЕДУРКУ надо переделать, так чтобы данные в dataGridView нормально отображались !!!!!!!!!!
          }
        

        private void Form3_Load(object sender, EventArgs e)
        {

            
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int idrow = 1;
            switch (tabControl1.SelectedIndex)
            {
              case 0:
                  idrow = dataGridView1.SelectedCells[0].RowIndex;
                  idVal = dataSet1.Tables[0].Rows[idrow][0].ToString();
                  textBox1.Text = dataSet1.Tables[0].Rows[idrow][1].ToString();
                  break;
              case 1:
                  idrow = dataGridView2.SelectedCells[0].RowIndex;
                  idVal = dataSet2.Tables[0].Rows[idrow][0].ToString(); 
                  textBox1.Text = dataSet2.Tables[0].Rows[idrow][1].ToString(); 
                  break;
              case 2:
                  idrow = dataGridView3.SelectedCells[0].RowIndex;
                  idVal = dataSet3.Tables[0].Rows[idrow][0].ToString();
                  textBox1.Text = dataSet3.Tables[0].Rows[idrow][1].ToString();
                  break;
             }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы действительно желаете удалить запись?", "Вопрос", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
              string tabSp = "Country";
              int idrow = 1;
            
              Con_DB cdb = new Con_DB(@"DBdecor.accdb");
              switch (tabControl1.SelectedIndex)
               {
                case 0:
                    idrow = dataGridView1.SelectedCells[0].RowIndex;
                    idVal = dataSet1.Tables[0].Rows[idrow][0].ToString();
                    tabSp = "Country";   
                    break;
                case 1:
                    idrow = dataGridView2.SelectedCells[0].RowIndex;
                    idVal = dataSet2.Tables[0].Rows[idrow][0].ToString(); 
                    tabSp = "Type_using";
                    break;
                case 2:
                    idrow = dataGridView3.SelectedCells[0].RowIndex;
                    idVal = dataSet3.Tables[0].Rows[idrow][0].ToString(); 
                    tabSp = "Move_info";
                    break;
                }

              if (idVal != "0")
              {
                  cdb.SetData("DELETE FROM " + tabSp + " where ID = " + idVal);
                  cdb.SetData("DELETE FROM " + tabSp + " where ID = " + idVal);
              }
                textBox1.Clear();
                textBox1.Focus();
                RefreshData();
               // dataGridView1.Refresh();
               // Form3 dd = new Form3();
                //this.Refresh();
                //this.Update();
               // dd.Update();
               // dd.Refresh();
               
              
                
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            // Эту процедуру тоже надо доделать как и RefreshData, все из за тогоже dataGridView!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            string fieldSp = "namec";
            string tabSp = "Country";
            if (textBox2.Text != "")
            {
                Con_DB cdb = new Con_DB(@"DBdecor.accdb");
                switch (tabControl1.SelectedIndex)
                {
                    case 0:
                        tabSp = "Country";
                        fieldSp = "namec";
                        dataSet1.Clear();
                        //dataGridView1.Columns.Clear();
                        cdb.GetData("Select * from " + tabSp + " where " + fieldSp + " like '%" + textBox2.Text + "%'", dataSet1);
                        dataGridView1.AutoGenerateColumns = true;
                        dataGridView1.DataSource = dataSet1;
                        dataGridView1.DataMember = dataSet1.Tables[0].TableName;
                       // dataGridView1.Update();
                        break;
                    case 1:
                        tabSp = "Type_using";
                        fieldSp = "nameu";
                        dataSet2.Clear();
                        dataGridView2.Columns.Clear();
                        cdb.GetData("Select * from " + tabSp + " where " + fieldSp + " like '%" + textBox2.Text + "%'", dataSet2);
                        dataGridView2.AutoGenerateColumns = true;
                        dataGridView2.DataSource = dataSet2;
                        dataGridView2.DataMember = dataSet2.Tables[0].TableName;
                        dataGridView2.Update();
                        break;
                    case 2:
                        tabSp = "Move_info";
                        fieldSp = "namep";
                        dataSet3.Clear();
                        dataGridView3.Columns.Clear();
                        cdb.GetData("Select * from " + tabSp + " where " + fieldSp + " like '%" + textBox2.Text + "%'", dataSet3);
                        dataGridView3.AutoGenerateColumns = true;
                        dataGridView3.DataSource = dataSet3;
                        dataGridView3.DataMember = dataSet3.Tables[0].TableName;
                        dataGridView3.Update();
                        break;
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
